package com.yunli.mall.common.exception;

import com.google.common.base.Joiner;
import lombok.Data;

/**
 * @author wangyue
 * @description
 * @date 2019-11-13 17:58
 */
@Data
public class MallRuntimeException extends RuntimeException implements ErrorDescription {

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误消息
     */
    private String message;

    /**
     * 构造
     *
     * @param code
     */
    public MallRuntimeException(Integer code) {
        super(ErrorCode.INTERNAL_ERROR.getMessage());
        this.message = ErrorCode.INTERNAL_ERROR.getMessage();
        this.code = code;
    }

    /**
     * 构造
     *
     * @param message
     */
    public MallRuntimeException(String message) {
        super(message);
        this.message = message;
        this.code = ErrorCode.INTERNAL_ERROR.getCode();
    }

    /**
     * 构造
     *
     * @param code
     * @param message
     */
    public MallRuntimeException(Integer code, String message) {
        super(message);
        this.message = message;
        this.code = code;
    }

    /**
     * 构造-取异常的message
     *
     * @param cause
     */
    public MallRuntimeException(Throwable cause) {
        super(cause);
        this.code = ErrorCode.INTERNAL_ERROR.getCode();
        this.message = cause.getMessage();
    }

    /**
     * 构造
     *
     * @param cause
     * @param code
     * @param message
     */
    public MallRuntimeException(Throwable cause, Integer code, String message) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }

    /**
     * 构造
     *
     * @param errorDescription
     */
    public MallRuntimeException(ErrorDescription errorDescription) {
        super(errorDescription.getMessage());
        this.message = errorDescription.getMessage();
        this.code = errorDescription.getCode();
    }

    /**
     * 构造
     * 使用","拼接appendMessages作为message
     *
     * @param errorDescription
     * @param appendMessages
     */
    public MallRuntimeException(ErrorDescription errorDescription, String... appendMessages) {
        super(errorDescription.getMessage().concat(":").concat(Joiner.on(",").join(appendMessages)));
        this.message = errorDescription.getMessage().concat(":").concat(Joiner.on(",").join(appendMessages));
        this.code = errorDescription.getCode();
    }

}
